package com.woniuxy.store.core.mapper;

import com.woniuxy.store.core.model.MdSf;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.store.core.model.MdStorefirst;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
public interface MdSfMapper extends BaseMapper<MdSf> {

    @Update("UPDATE `md_sf` SET `flag`=1 WHERE `sf_id`=#{sfId}")
    void delectOne(int sfId);

    @Update("update md_sf set flag=CASE WHEN flag=0 THEN 1 ELSE 0 END where first_id=#{firstId} and store_id=#{storeId}")
    void updateFlag(Integer storeId, Integer firstId);

    @Insert("insert into md_sf (store_id,first_id) values (#{storeId},#{firstId})")
    void fabuf(Integer storeId, Integer firstId);

    @Insert("insert into md_ss (store_id,second_id) values (#{storeId},#{secondId})")
    void fabus(Integer storeId, Integer secondId);

    @Insert("insert into md_st (store_id,third_id) values (#{storeId},#{thirdId})")
    void fabut(Integer storeId, Integer thirdId);

    @Select("SELECT DISTINCT f.first_id,f.sf_id,f.flag, GROUP_CONCAT( i.first_name ORDER BY i.first_id)  as  firstServe  from md_sf f LEFT JOIN md_storefirst i ON FIND_IN_SET( i.first_id, f.first_id)  WHERE f.store_id=#{storeId} GROUP BY f.sf_id")
    List<MdSf> selectFirstServe(Integer storeId);

    @Update("update md_sf set flag=CASE WHEN flag=0 THEN 1 ELSE 0 END where sf_id=#{id}")
    void updateFlagById(Integer id);


    @Select("SELECT COUNT(*) as quantityOnShelves FROM md_sf WHERE store_id=#{storeId} AND flag=1")
    Integer quantityOnShelves(Integer storeId);

    @Select("SELECT COUNT(*) as quantityOffTheShelf FROM md_sf WHERE store_id=#{storeId} AND flag=0")
    Integer quantityOffTheShelf(Integer storeId);

    @Select("select count(*) as carwash from md_sf where first_id=#{firstId}")
    Integer count(Integer firstId);
}
